using System;
using Core.Extensions;


namespace SolarDevices
{
    public class OutbackLookupTables
    {
        static readonly Tupple[] InverterError =
            new[]{
                new Tupple { Description = "Low VAC output.", Value = 1 },
                new Tupple { Description = "Stacking Error.", Value = 2 },
                new Tupple { Description = "Over temperature.", Value = 4 },
                new Tupple { Description = "Low battery.", Value = 8 },
                new Tupple { Description = "Phase loss.", Value = 16 },
                new Tupple { Description = "High battery.", Value = 32 },
                new Tupple { Description = "Shorted output.", Value = 64 },
                new Tupple { Description = "Back feed.", Value = 128 }
            };

        static readonly Tupple[] BatteryStage =
            new[]{
                new Tupple { Description = "(Off) No power, waiting for power source, battery voltage over set point.", Value = 0 },
                new Tupple { Description = "(Bulk) Max power point tracking until absorb voltage reached.", Value = 2 },
                new Tupple { Description = "(Float) Battery is full and regulating battery voltage at float set point.", Value = 1 },
                new Tupple { Description = "(Absorb) Max power point tracking. Seeking float set point voltage.", Value = 3 },
                new Tupple { Description = "(Equalize) Max power point tracking. Seeking equalize set point voltage.", Value = 4 }
            };

        static readonly Tupple[] ErrorModes =
            new[]{
                new Tupple { Description = "Shorted battery sensor.", Value = 32 },
                new Tupple { Description = "Too Hot.", Value = 64 },
                new Tupple { Description = "High VOC.", Value = 128 }
            };


        public static string LookupInverterError(UInt32 infoFlag)
        {
            var b = new StringBuilder("");
            bool first = true;
            foreach (var infoItem in InverterError)
            {
                var bit = infoFlag & infoItem.Value;
                if (bit != 0)
                {
                    if (!first)
                    {
                        b.Append(" - ");
                    }
                    else
                    {
                        first = false;
                    }
                    b.Append(infoItem.Description);
                }
            }
            return b.ToString();
        }

        public static string LookupControllerError(UInt32 infoFlag)
        {
            var b = new StringBuilder("");
            bool first = true;
            foreach (var infoItem in ErrorModes)
            {
                var bit = infoFlag & infoItem.Value;
                if (bit != 0)
                {
                    if (!first)
                    {
                        b.Append(" - ");
                    }
                    else
                    {
                        first = false;
                    }
                    b.Append(infoItem.Description);
                }
            }
            return b.ToString();
        }

        public static string LookupBatteryStage(int val)
        {
            foreach (var infoMap in BatteryStage)
            {
                if (val == infoMap.Value)
                {
                    return infoMap.Description;
                }
            }
            return "Undefined";
        }
    }
}
